/*
** Copyright (c) 2008 Mobile GCalendarManager
**
** Permission is hereby granted, free of charge, to any person
** obtaining a copy of this software and associated documentation
** files (the "Software"), to deal in the Software without
** restriction, including without limitation the rights to use,
** copy, modify, merge, publish, distribute, sublicense, and/or sell
** copies of the Software, and to permit persons to whom the
** Software is furnished to do so, subject to the following
** conditions:
**
** The above copyright notice and this permission notice shall be
** included in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
** HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
** FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
** OTHER DEALINGS IN THE SOFTWARE.
*/

/**
 *
 */
package au.edu.mq.comp.itec800.mgc.lib;

import org.apache.log4j.Logger;

import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;

/**
 * Wrapper class to manager Google Calendar accounts when interacting with
 * the Google Calendar Service.
 * Each instance of GAccount holds a reference to a CalendarService object
 * after authentication has succeeded, allowing applications to manage
 * multiple user-connections at the same time.
 *
 *
 * @author Laurent Malvert [laurent.malvert@students.mq.edu.au]
 */
public class GAccount
{
  private static final Logger logger       = Logger.getLogger(GAccount.class);

  private GCalendarService     gcs         = null;
  private GCalendarManager     calendarMgr = null;
  private String               user        = null;
  private boolean              connected   = false;


  /**
   * application string for Google Services should be of the form:
   *  companyName-applicationName-versionId
   * like:
   *  mq_itec800_2008_1-mgc-0.1.r357
   *
   * @param user
   *            the username for the desired Google Account
   */
  public GAccount(final String user)
    throws ServiceException
  {
    if (user == null)
    {
      throw new IllegalArgumentException("username cannot be null");
    }
    this.user = user;
    gcs       = new GCalendarService();
    logger.debug(" * instantiated GAccount [" + getUser() + "]");
  }

  public boolean              authenticate(final String password)
    throws ServiceException
  {
    logger.info("authentication trial for [" + user + "]");
    if (password == null)
    {
      throw new AuthenticationException("Credentials cannot be null values.");
    }
    getService().setUserCredentials(user, password);
    connected = true;
    logger.debug(" authentication succeeded");
    return (connected);
  }

  public String               getUser()
  {
    return (user);
  }

  public String               getEmail()
  {
    if (user.indexOf('@') >= 0)
      return (user);
    return (user + "@gmail.com");
  }

  public CalendarService      getService()
  {
    return (gcs);
  }

  public boolean              isAuthenticated()
  {
    return (connected);
  }

  public GCalendarManager     getCalendarManager()
  {
    if (calendarMgr == null)
    {
      calendarMgr = new GCalendarManager(this);
    }
    return (calendarMgr
        );
  }
}
